#!/bin/bash -ex

if [ $# -ne 3 ]; then
	exit
fi

if [ ${1} == ${2} ]; then
	exit
fi

dir=$(pwd)

if [ -d ${3} ]; then
	if [ `ls | wc -l` -eq 0 ]; then
		cd ${3}
	else
		echo "${3}目录非空"
		exit
	fi
else
	mkdir ${3}
	cd ${3}
fi

wget -c https://mirrors.tuna.tsinghua.edu.cn/kernel/v${1:0:1}.x/linux-${1}.tar.xz
if [ -d linux-${1} ]; then
	echo "目录存在"
else
	tar xvf linux-${1}.tar.xz
fi

wget -c https://mirrors.tuna.tsinghua.edu.cn/kernel/v${2:0:1}.x/linux-${2}.tar.xz
if [ -d linux-${2} ]; then
	echo "目录存在"
else
	tar xvf linux-${2}.tar.xz
fi

cd linux-${1}
if [ -d Kconfiglib ]; then
	echo "目录存在"
else
	git clone git://github.com/ulfalizer/Kconfiglib.git
	patch -p1 < Kconfiglib/makefile.patch
	ln -sf ${dir}/kernel/config/config_script.py
	make defconfig
fi
cd ..

cd linux-${2}
if [ -d Kconfiglib ]; then
	echo "目录存在"
else
	git clone git://github.com/ulfalizer/Kconfiglib.git
	patch -p1 < Kconfiglib/makefile.patch
	ln -sf ${dir}/kernel/config/config_script.py
	make defconfig
fi
cd ..

ln -sf ${dir}/kernel/config/compare.py

cat > clean.sh << EOF
#!/bin/bash -ex

rm -rf ./*.json
rm -rf ${1}/kconfig.json
rm -rf ${2}/kconfig.json
EOF

chmod +x clean.sh

